import { createRouter, createWebHistory,createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { automaticLogin } from '@/api/api' // 根据实际路径修改
import { ElMessage } from 'element-plus'
const routes: Array<RouteRecordRaw> = [
	{
		path: '/',
		name: 'index',
		component: () => import('@/views/index.vue'),
		meta: {
			title: '首页',
		},
	},
	{
		path: '/course/:comn/:courseid',
		name: 'course',
		component: () => import('@/views/course.vue'),
		meta: {
			title: '课程内容',
		},
	},
	{
		path: '/course/:coureid?',
		name: 'courselist',
		component: () => import('@/views/CourseList.vue'),
		meta: {
			title: '课程列表',
		},
	},
	{
		path: '/search/:type/:keyword',
		name: 'search',
		component: () => import('@/views/search.vue'),
		meta: {
			title: '搜索',
		},
	},
	{
		path: '/article/:comn/:articid',
		name: 'article',
		component: () => import('@/views/article.vue'),
		meta: {
			title: '文章内容',
		},
	},
	{
		path: '/column/:comn',
		name: 'column',
		component: () => import('@/views/column.vue'),
		meta: {
			title: '栏目',
		},
	},
	{
		path: '/:catchAll(.*)',
		name: '错误',
		component: () => import('@/views/error.vue'),
		meta: {
			title: '404',
		},
	},
	{
		path: '/homepage/:userid',
		name: 'homepage',
		component: () => import('@/views/homepage.vue'),
		meta: {
			title: '个人主页',
		},
	},
	{
		path: '/question',
		name: 'question',
		component: () => import('@/views/question.vue'),
		meta: {
			title: '问答社区',
		},
	},
	{
		path: '/qaarticle/:comn/:index',
		name: 'qaarticle',
		component: () => import('@/views/qaarticle.vue'),
		meta: {
			title: '问题',
		},
	},
	{
		path: '/askaquestion',
		name: 'askaquestion',
		component: () => import('@/views/askaquestion.vue'),
		meta: {
			title: '发起提问',
		},
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('@/views/register.vue'),
		meta: {
			title: '用户注册',
		},
	},
	{
		path: '/user',
		name: 'user',
		component: () => import('@/user/index.vue'),
		meta: {
			title: '我的账户',
		},
	},

	{
		path: '/user/mycourse',
		name: 'mycourse',
		component: () => import('@/user/course.vue'),
		meta: {
			title: '我的课程',
		},
	},
	{
		path: '/user/order',
		name: 'order',
		component: () => import('@/user/order.vue'),
		meta: {
			title: '我的订单',
		},
	},
	{
		path: '/user/MyCollection',
		name: 'MyCollection',
		component: () => import('@/user/MyCollection.vue'),
		meta: {
			title: '我的收藏',
		},
	},
	{
		path: '/user/forum',
		name: 'forum',
		component: () => import('@/user/forum.vue'),
		meta: {
			title: '我的帖子',
		},
	},
	{
		path: '/user/question',
		name: 'myquestion',
		component: () => import('@/user/question.vue'),
		meta: {
			title: '我的提问',
		},
	},
	{
		path: '/user/cat',
		name: 'cat',
		component: () => import('@/user/cat.vue'),
		meta: {
			title: '在线客服',
		},
	},
	{
		path: '/user/settings',
		name: 'settings',
		component: () => import('@/user/settings.vue'),
		meta: {
			title: '个人资料',
		},
	},
	{
		path: '/login',
		name: 'gitub',
		component: () => import('@/views/login.vue'),
		meta: {
			title: 'gitub登陆',
		},
	},
]
// 线上
const router = createRouter({
	history: createWebHistory(process.env.BASE_URL),
	routes,
})
// APP
// const router = createRouter({
//     history: createWebHashHistory(),
//     routes,
// })
// https://www.python100.com/html/108162.html
// 全局前置守卫

// 全局前置守卫
let previousRoute = null
router.beforeEach(async (to, from, next) => {
	// 将当前路径和来源路径分解为数组，去除空字符串
	previousRoute = from // 在每次路由跳转前记录上一页的路由信息
	let currentPathArray = to.path.split('/').filter(Boolean)
	let fromPathArray = from.path.split('/').filter(Boolean)

	// 定义需要登录验证的页面路径
	const requiresLoginPages = ['user', 'askaquestion']

	if (requiresLoginPages.includes(currentPathArray[0]) || requiresLoginPages.includes(fromPathArray[0])) {
		console.log('验证后登陆')
		let loginResult = await automaticLogin()
		if (loginResult.code === 200) {
			// 登录成功，继续导航
			next()
		} else {
			let errorMessage = ''
			if (loginResult.code === 498) {
				errorMessage = '账号在其他地方登陆'
			} else if (loginResult.code === 401) {
				errorMessage = '授权已经过期'
			} else {
				errorMessage = '请先登录'
			}
			ElMessage.error(errorMessage)

			// 判断用户来自的前一个页面是否需要登录验证
			if (requiresLoginPages.includes(fromPathArray[0])) {
				next('/') // 来自需要登录验证的页面，跳转到首页
			} else {
				next(from.path) // 来自不需要登录验证的页面，返回前一个页面
			}
		}
	} else {
		// 无需登录验证的页面，直接执行登录函数，不等待结果
		// 继续导航到目标页面
		next()
		automaticLogin()
		console.log('直接访问')
		// 继续导航到目标页面

	}
})

export default router
export { previousRoute }
